package es.biciapp.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import es.biciapp.logic.Bici;

public class QueryBici {

	public static List<Bici> getBicis() {
		List<Bici> bicis = new ArrayList<Bici>();
		Bici actual = new Bici();
		Connection conn;
		PreparedStatement ps;
		String url;
		ResultSet rs;
		try {
			Class.forName("org.sqlite.JDBC");
			url = "jdbc:sqlite:BiciApp.db";
			conn = DriverManager.getConnection(url, "", "");
			ps = conn.prepareStatement("SELECT * FROM BICI ;");
			rs = ps.executeQuery();
			while (rs.next()) {
				actual = new Bici();
				actual.setId(rs.getInt("BICI_ID"));
				actual.setMarca(rs.getString("MARCA"));
				actual.setModelo(rs.getString("MODELO"));
				actual.setCaracteristicas(rs.getString("CARACTERISTICAS"));
				actual.setNickUsuario(rs.getString("NICK_USUARIO"));
				bicis.add(actual);
			}
			conn.close();
			ps.close();
			rs.close();
			return bicis;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error en la consulta getBicis()");
			return new ArrayList<Bici>();
		}
	}

	public static void main(String[] args) {
		CreateDB.createTableBici();
		System.out.println(QueryBici.getBicis().toString());
	}
}
